<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="计划开始时间" prop="startTime">
            <el-date-picker
              v-model="formItem.startTime"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              :picker-options="pickerOptions"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="计划结束时间" prop="endTime">
            <el-date-picker
              v-model="formItem.endTime"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              :picker-options="pickerOptions"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否中断业务" required prop="interruptBusiness">
            <el-radio v-for="(item) in interruptBusiness" :key="item.value" v-model="formItem.interruptBusiness" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否重大变更" required prop="significantChange">
            <el-radio v-for="(item) in significantChange" :key="item.value" v-model="formItem.significantChange" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="可能存在的风险" prop="risk">
            <!-- <Tinymce ref="tinymce" v-model="formItem.risk" :height="200" menubar="false" /> -->
            <el-input v-model="formItem.risk" placeholder="请输入内容" :rows="10" :maxlength="2047" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="风险处置措施" prop="riskManagement">
            <!-- <Tinymce ref="tinymce" v-model="formItem.riskManagement" :height="200" menubar="false" /> -->
            <el-input v-model="formItem.riskManagement" placeholder="请输入内容" :rows="10" :maxlength="2047" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="实施方案" prop="implementPlan">
            <!-- <Tinymce ref="tinymce" v-model="formItem.implementPlan" :height="200" menubar="false" /> -->
            <el-input v-model="formItem.implementPlan" placeholder="请输入内容" :rows="10" :maxlength="2047" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="回退方案" prop="fallbackPlan">
            <!-- <Tinymce ref="tinymce" v-model="formItem.fallbackPlan" :height="200" menubar="false" /> -->
            <el-input v-model="formItem.fallbackPlan" placeholder="请输入内容" :rows="10" :maxlength="2047" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="附件">
            <el-upload
              class="upload-demo"
              :headers="headers"
              :action="importUrl"
              :file-list="formItem.attachments"
              :before-upload="handleBeforeUpLoad"
              :on-success="handleSuccess"
              :on-error="handleError"
              :on-remove="handleRemove"
              :show-file-list="true"
            >
              <el-button type="primary" icon="el-icon-upload2">上传</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <div style="text-align: right;">
        <el-form-item>
          <el-button type="primary" @click="ok('submit')">提交</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  importUrl,
  deleteAttachmentByFsId
} from '@neu/dcom-ui/api/changePlan'
// import Tinymce from '@neu/biz-core/components/Tinymce'
import { getToken } from '@neu/biz-core/utils/auth'

const formItem = {
  startTime: '',
  endTime: '',
  interruptBusiness: false,
  significantChange: false,
  risk: '',
  riskManagement: '',
  implementPlan: '',
  fallbackPlan: '',
  attachments: []
}
export default {
  name: 'ChangePlan',
  components: {
    // Tinymce
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      interruptBusiness: [{ value: true, label: '是' }, { value: false, label: '否' }],
      significantChange: [{ value: true, label: '是' }, { value: false, label: '否' }],
      /** 上传路径 */
      importUrl: importUrl,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      /** 是否显示用户选择对话框 */
      userSelDialogFormVisible: false,
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        startTime: [{
          required: true,
          message: '计划开始时间不能为空',
          trigger: 'change'
        }],
        endTime: [{
          required: true,
          message: '计划结束时间不能为空',
          trigger: 'change  '
        }],
        interruptBusiness: [{
          required: true,
          message: '是否中断业务不能为空',
          trigger: 'change  '
        }],
        significantChange: [{
          required: true,
          message: '是否重大变更不能为空',
          trigger: 'change'
        }],
        risk: [{
          required: true,
          message: '可能存在的风险不能为空',
          trigger: 'blur'
        }],
        riskManagement: [{
          required: true,
          message: '风险处置措施不能为空',
          trigger: 'change'
        }],
        implementPlan: [{
          required: true,
          message: '实施方案不能为空',
          trigger: 'change'
        }],
        fallbackPlan: [{
          required: true,
          message: '回退方案不能为空',
          trigger: 'change'
        }]
      }
    }
  },
  watch: {
  },
  created() { },
  methods: {
    ok(value) {
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            this.formItem.workOrderNum = this.workOrderNum
            this.formItem.taskId = this.taskId
            create(this.formItem).then(res => {
              if (res.status === 'success') {
                this.$emit('cancelNewOrderForm', 'refresh')
              }
            })
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    },
    handleSuccess(response, file, fileList) {
      if (response.status === 'failure') {
        this.$message({
          showClose: true,
          message: response.data,
          type: 'error'
        })
      } else {
        this.$message({
          showClose: true,
          message: '文件上传成功',
          type: 'success'
        })
        const obj = {
          fsId: response.data.fsId,
          name: response.data.name
        }
        const fsIdArr = this.formItem.attachments.map(t => t.fsId)
        if (!fsIdArr.includes(obj.fsId)) {
          this.formItem.attachments.push(obj)
        } else {
          fileList.pop()
        }
      }
    },
    handleError(err, file, fileList) {
      console.log(err)
    },
    handleRemove(file, fileList) {
      deleteAttachmentByFsId(file.fsId, this.id).then(res => {
        if (res.status === 'failure') {
          this.$message({
            showClose: true,
            message: res.data,
            type: 'error'
          })
        } else {
          this.formItem.attachments.splice(this.formItem.attachments.indexOf(file), 1)
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
        }
      })
    },
    handleBeforeUpLoad(file) {}
  }
}
</script>
